package cn.wenhe9.yygh.hosp.receiver;

import cn.wenhe9.yygh.common.result.ResultResponse;
import cn.wenhe9.yygh.hosp.producer.SmsProducer;
import cn.wenhe9.yygh.hosp.service.ScheduleService;
import cn.wenhe9.yygh.model.hosp.Schedule;
import cn.wenhe9.yygh.model.vo.sms.SmsVo;
import cn.wenhe9.yygh.model.vo.user.OrderMqVo;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author DuJinliang
 * 2022/4/8
 */
@Component
@EnableBinding(Sink.class)
public class HospitalReceiver {
    @Resource
    private ScheduleService scheduleService;

    @Resource
    private SmsProducer smsProducer;

    @StreamListener(Sink.INPUT)
    public void receiver(Message<OrderMqVo> message){
        OrderMqVo orderMqVo = message.getPayload();
        //下单成功更新预约数
        Schedule schedule = scheduleService.getScheduleById(orderMqVo.getScheduleId());
        schedule.setReservedNumber(orderMqVo.getReservedNumber());
        schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
        scheduleService.updateSchedule(schedule);
        //发送短信
        SmsVo smsVo = orderMqVo.getSmsVo();
        if (null != smsVo){
            smsProducer.sendSms(smsVo);
        }
    }
}
